package com.i8office.android.zhihuixinwu.ui.schedule;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.i8office.android.zhihuixinwu.R;
import com.socks.library.KLog;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import view.recyclerview.CommonAdapter;
import view.recyclerview.ViewHolder;

public class CalendarView extends FrameLayout implements View.OnClickListener {
    private View mLeftButton;
    private View mRightButton;
    private View mMonthLayout;
    private View mWeekLayout;
    private View mEmpty;
    TextView mMonthTitle;
    TextView mLeader;
    View mLeaderLayout;
    RecyclerView mDayRecyclerView;
    private CommonAdapter<DayItem> mAdapter;
    //    private SimpleDateFormat format = null;
//    private SimpleDateFormat format1 = null;
    private String mCurrentDate;
    private OnCalandarChangedListener onCalandarChangedListener;
    private Calendar mCurrentCalendar;
    private DayItem currentDayItem;

    public CalendarView(@NonNull Context context) {
        this(context, null);
    }

    public CalendarView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CalendarView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View rootView = LayoutInflater.from(context).inflate(R.layout.view_calendar, null);
        mMonthLayout = rootView.findViewById(R.id.month_layout);
        mWeekLayout = rootView.findViewById(R.id.week_layout);
        mMonthTitle = rootView.findViewById(R.id.monthText);
        mDayRecyclerView = rootView.findViewById(R.id.rv_day_list);
        mEmpty = rootView.findViewById(R.id.empty);
        mLeader = rootView.findViewById(R.id.leader);
        mLeaderLayout = rootView.findViewById(R.id.leader_layout);
        mLeftButton = rootView.findViewById(R.id.leftButton);
        mRightButton = rootView.findViewById(R.id.rightButton);
        mLeftButton.setOnClickListener(this);
        mRightButton.setOnClickListener(this);
        this.addView(rootView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        init();
    }

    private void init() {
        mCurrentCalendar = Calendar.getInstance();
        currentDayItem = new DayItem();
        currentDayItem.setYear(mCurrentCalendar.get(Calendar.YEAR));
        currentDayItem.setMonth(mCurrentCalendar.get(Calendar.MONTH) + 1);
        currentDayItem.setDay(mCurrentCalendar.get(Calendar.DAY_OF_MONTH));
        mCurrentDate = currentDayItem.getDate();
        mMonthTitle.setText(String.format("%d年%d月", mCurrentCalendar.get(Calendar.YEAR), (mCurrentCalendar.get(Calendar.MONTH) + 1)));
        mDayRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 7));
        mDayRecyclerView.setAdapter(mAdapter = new CommonAdapter<DayItem>(getContext(), R.layout.item_day) {
            @Override
            protected void convert(ViewHolder holder, final DayItem dayItem, final int position) {
                ImageView mMark = holder.getView(R.id.mark);
                if (dayItem.getStatus().equals("empty")) {
                    mMark.setVisibility(View.GONE);
                } else {
                    mMark.setVisibility(View.VISIBLE);
                    mMark.setImageResource(currentDayItem.getDate().equals(dayItem.getDate()) ? R.drawable.circle_white_bg : R.drawable.circle_bg);
                }
                holder.setBackgroundRes(R.id.bg, currentDayItem.getDate().equals(dayItem.getDate()) ? R.drawable.circle_bg : R.color.transparent)
                        .setText(R.id.textView, String.valueOf(dayItem.getDay()));
                TextView dayText = holder.getView(R.id.textView);
                if (currentDayItem.getDate().equals(dayItem.getDate())) {
                    dayText.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
                } else {
                    if (mCurrentDate.equals(dayItem.getDate())) {
                        dayText.setTextColor(ContextCompat.getColor(getContext(), R.color.blue));
                    } else if (dayItem.isIsInMonth()) {
                        dayText.setTextColor(ContextCompat.getColor(getContext(), R.color.black));
                    } else {
                        dayText.setTextColor(ContextCompat.getColor(getContext(), R.color.holo_gray));
                    }
                }
                holder.getConvertView().setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        KLog.e("check date:" + dayItem.getDate());
                        currentDayItem = dayItem;
                        if (!dayItem.isIsInMonth()) {
                            if (position < 7) {
                                mLeftButton.performClick();
                            } else {
                                mRightButton.performClick();
                            }
                        }
                        if (onCalandarChangedListener != null) {
                            onCalandarChangedListener.onCheckDate(dayItem);
                        }
                        notifyDataSetChanged();
                    }
                });
            }
        });
    }

    public void setLeader(String leaderName) {
        mLeader.setText("当前领导：" + leaderName);
        mLeaderLayout.setVisibility(View.VISIBLE);
    }

    public void setOnLeaderClick(View.OnClickListener onLeaderClick) {
        mLeaderLayout.setOnClickListener(onLeaderClick);
    }

    public void setEmpty(boolean showEmpty) {
        mEmpty.setVisibility(showEmpty ? View.VISIBLE : View.GONE);
    }

    //获取当前年
    public int getCurrentYear() {
        return mCurrentCalendar.get(Calendar.YEAR);
    }

    //获取当前月
    public int getCurrentMonth() {
        return mCurrentCalendar.get(Calendar.MONTH) + 1;
    }

    public String getCheckedDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(currentDayItem.getYear(), currentDayItem.getMonth() - 1, currentDayItem.getDay());
        return format.format(calendar.getTimeInMillis());
    }

    public void setData(List<DayItem> data) {
        mMonthLayout.setVisibility(View.VISIBLE);
        mWeekLayout.setVisibility(View.VISIBLE);
        mAdapter.setList(data);
        if (onCalandarChangedListener != null) {
            onCalandarChangedListener.onCheckDate(currentDayItem);
        }
    }

    public void setOnCalandarChangedListener(OnCalandarChangedListener onCalandarChangedListener) {
        this.onCalandarChangedListener = onCalandarChangedListener;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.leftButton:
                mCurrentCalendar.add(Calendar.MONTH, -1);
                setCurrentMonth();
                if (onCalandarChangedListener != null) {
                    onCalandarChangedListener.onMonthChanged(getCurrentYear(), getCurrentMonth());
                }
                break;
            case R.id.rightButton:
                mCurrentCalendar.add(Calendar.MONTH, 1);
                setCurrentMonth();
                if (onCalandarChangedListener != null) {
                    onCalandarChangedListener.onMonthChanged(getCurrentYear(), getCurrentMonth());
                }
                break;
        }
    }

    private void setCurrentMonth() {
        mMonthTitle.setText(String.format("%d年%d月", mCurrentCalendar.get(Calendar.YEAR), (mCurrentCalendar.get(Calendar.MONTH) + 1)));
    }

    public interface OnCalandarChangedListener {
        void onCheckDate(DayItem item);

        void onMonthChanged(int year, int month);
    }
}
